/**
 * 
 */
package com.witframework.core.persistence;



import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.witframework.core.ApplicationProperties;
import com.witframework.core.ServiceCallback;
import com.witframework.core.ServiceCallbackWithoutResult;
import com.witframework.core.TransactionTemplate;
import com.witframework.core.WitFrameWork;
/**
 * 一个用于直接的使用的Hibernate Dao，不应直接使用该类，应通过WitFrameWork.getBaseHibernateDao()
 * 使用该类
 * @see com.witframework.core.WitFrameWork#getBaseHibernateDao(Class)
 * @author wuxinyang
 * Jun 30, 2009 9:09:33 AM
 * 
 */
public class SimpleHibernateDao<T> implements ISimpleHibernateDao<T>{
	
	protected boolean cache=false;
	protected Class<T> entityClass=null;// DAO所管理的Entity类型.
	public SimpleHibernateDao(){
		super();
	}	
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#setEntityClass(java.lang.Class, boolean)
	 */
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#setEntityClass(java.lang.Class, boolean)
	 */
	public void setEntityClass(Class clazz,boolean noMem) {
		entityClass=(Class<T>)clazz;
		String cacheClasses=ApplicationProperties.getProperty("hibernate.cache.class","");		
		if(noMem){
			cache=false;
		}	
		else if((entityClass!=null&&cacheClasses.indexOf(entityClass.getName())>-1)){
			cache=true;
		}
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#getDao()
	 */
	private CommonHibernateDao getDao(){		
		return (CommonHibernateDao)WitFrameWork.getBean("commonHibernateDao")	;
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#delete(java.lang.Object)
	 */
	public void delete(final Object o) throws DataAccessException {
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().delete(cache,o);						
					}
		});		
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#delete(java.lang.Object, com.witframework.core.ServiceCallback)
	 */
	public void delete(final Object o,final ServiceCallback callback) throws DataAccessException {
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().delete(cache,o,callback);						
					}
		});		
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteAll(java.util.Collection)
	 */
	public void deleteAll(final Collection entities) {
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteAll(cache, entities);			
					}
		});		
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteAll(java.util.Collection, com.witframework.core.ServiceCallback)
	 */
	public void deleteAll(final Collection entities,final ServiceCallback callback) {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteAll(cache, entities);		
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteAll()
	 */
	public void deleteAll() {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteAll(cache, entityClass);		
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteAll(com.witframework.core.ServiceCallback)
	 */
	public void deleteAll(final ServiceCallback callback) {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteAll(cache, entityClass,callback);	
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteAllByIds(java.lang.String)
	 */
	public void deleteAllByIds(final String... ids) throws DataAccessException {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteAllByIds(cache, entityClass, ids);
					}
		});
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteAllByIds(com.witframework.core.ServiceCallback, java.lang.String)
	 */
	public void deleteAllByIds(final ServiceCallback callback,final String... ids) throws DataAccessException {
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteAllByIds(cache, entityClass,callback, ids);
					}
		});
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteBy(java.lang.String, java.lang.Object)
	 */
	public void deleteBy(final String propertyName,final Object value) {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteBy(cache, entityClass, propertyName, value);
					}
		});
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteBy(java.lang.String, java.lang.Object, com.witframework.core.ServiceCallback)
	 */
	public void deleteBy(final String propertyName,final  Object value,final ServiceCallback callback) {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteBy(cache, entityClass, propertyName, value,callback);
					}
		});
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteById(java.io.Serializable)
	 */
	public void deleteById(final Serializable id) throws DataAccessException {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteById(cache, entityClass, id);
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#deleteById(java.io.Serializable, com.witframework.core.ServiceCallback)
	 */
	public void deleteById(final Serializable id,final ServiceCallback callback) throws DataAccessException {
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().deleteById(cache, entityClass, id,callback);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findBy(java.util.Map)
	 */
	public List findBy(final Map filter) {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findBy(cache, entityClass, filter);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findBy(java.util.Map, java.lang.String, boolean)
	 */
	public List findBy(final Map filter,final String orderBy,final boolean isAsc) {		
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findBy(entityClass, filter, orderBy, isAsc);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findBy(java.lang.String, java.lang.Object)
	 */
	public List findBy(final String propertyName,final Object value) {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findBy(cache, entityClass, propertyName, value);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findById(java.io.Serializable)
	 */
	public Object findById(final Serializable id) throws DataAccessException {
		return (Object) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findById(cache, entityClass, id);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findById(java.io.Serializable, boolean)
	 */
	public Object findById(final Serializable id,final boolean lock) throws DataAccessException {	
		
		return (Object) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findById(cache, entityClass, id, lock);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findByNamedQuery(java.lang.String)
	 */
	public List findByNamedQuery(final String queryName) throws DataAccessException {			
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findByNamedQuery(queryName);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findByNamedQuery(java.lang.String, java.lang.Object)
	 */
	public List findByNamedQuery(final String queryName,final Object value) throws DataAccessException {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findByNamedQuery(queryName, value);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findByNamedQuery(java.lang.String, java.lang.Object[])
	 */
	public List findByNamedQuery(final String queryName,final Object[] values) throws DataAccessException {
		
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findByNamedQuery(queryName, values);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findByNamedQueryAndNamedParam(java.lang.String, java.lang.String, java.lang.Object)
	 */
	public List findByNamedQueryAndNamedParam(final String queryName,final String paramName,final Object value) {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findByNamedQueryAndNamedParam(queryName, paramName, value);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#findByNamedQueryAndNamedParam(java.lang.String, java.lang.String[], java.lang.Object[])
	 */
	public List findByNamedQueryAndNamedParam(final String queryName,final String[] paramNames,final Object[] values) throws DataAccessException {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().findByNamedQueryAndNamedParam(queryName, paramNames, values);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#getAll()
	 */
	public List getAll() throws DataAccessException {		
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().getAll(cache, entityClass);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#getAllSize()
	 */
	public int getAllSize() throws DataAccessException {		
		return (Integer)new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().getAllSize(entityClass);
					}
		});		
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#getBy(java.lang.String, java.lang.Object)
	 */
	public Object getBy(final String propertyName,final Object value) {	
		
		return (Object) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().getBy(cache, entityClass, propertyName, value);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#getEntityClass()
	 */
	public Class getEntityClass() {		
		return this.entityClass;
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#getHibernateTemplate()
	 */
	public HibernateTemplate getHibernateTemplate() {		
		return null;
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#isUnique(java.lang.Object, java.lang.String)
	 */
	public boolean isUnique(final Object entity,final String uniquePropertyNames) {
		return (Boolean) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().isUnique(entity, uniquePropertyNames);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#merge(java.lang.Object)
	 */
	public Object merge(final Object o) {			
		return (Object) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().merge(o);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#pagedQuery(java.util.Map, com.witframework.core.persistence.PageSortModel)
	 */
	public List pagedQuery(final Map filter,final PageSortModel psm) {		
		
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().pagedQuery(cache, entityClass, filter, psm);
					}
		});
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#pagedQueryByNamedQuery(java.lang.String, com.witframework.core.persistence.PageSortModel)
	 */
	public List pagedQueryByNamedQuery(final String queryName,final PageSortModel psm) throws DataAccessException {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().pagedQueryByNamedQuery(queryName, psm);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#pagedQueryByNamedQuery(java.lang.String, java.lang.Object, com.witframework.core.persistence.PageSortModel)
	 */
	public List pagedQueryByNamedQuery(final String queryName,final Object value,final PageSortModel psm) throws DataAccessException {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().pagedQueryByNamedQuery(queryName, value, psm);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#pagedQueryByNamedQuery(java.lang.String, java.lang.Object[], com.witframework.core.persistence.PageSortModel)
	 */
	public List pagedQueryByNamedQuery(final String queryName,final Object[] values,final PageSortModel psm) throws DataAccessException {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().pagedQueryByNamedQuery(queryName, values, psm);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#pagedQueryByNamedQueryAndNamedParam(java.lang.String, java.lang.String, java.lang.Object, com.witframework.core.persistence.PageSortModel)
	 */
	public List pagedQueryByNamedQueryAndNamedParam(final String queryName,final String paramName,final Object value,final PageSortModel psm) {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().pagedQueryByNamedQueryAndNamedParam(queryName, paramName, value, psm);
					}
		});
		
	}

	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#pagedQueryByNamedQueryAndNamedParam(java.lang.String, java.lang.String[], java.lang.Object[], com.witframework.core.persistence.PageSortModel)
	 */
	public List pagedQueryByNamedQueryAndNamedParam(final String queryName,final  String[] paramNames,final  Object[] values,final  PageSortModel psm) {
		return (List) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().pagedQueryByNamedQueryAndNamedParam(queryName, paramNames, values, psm);
					}
		});
		
	}
	
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#save(java.lang.Object, com.witframework.core.ServiceCallback)
	 */
	public String save(final Object entity,final ServiceCallback callback) throws DataAccessException {		
		return (String) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().save(cache, entity,callback);
					}
		});
		
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#save(java.lang.Object)
	 */
	public String save(final Object entity) throws DataAccessException {		
		return (String) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().save(cache,entity,null);
					}
		});
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#saveAll(java.util.Collection)
	 */
	public void saveAll(final Collection entityCollection) throws DataAccessException {		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().saveAll(cache, entityClass, entityCollection);
					}
		});
		
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#saveAll(java.util.Collection, com.witframework.core.ServiceCallback)
	 */
	public void saveAll(final Collection entityCollection,final ServiceCallback callback) throws DataAccessException {
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().saveAll(cache, entityClass, entityCollection,callback);
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#saveOrUpdate(java.lang.Object)
	 */
	public String saveOrUpdate(final Object entity) throws DataAccessException {
		
		return (String) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().saveOrUpdate(cache, entity);	
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#saveOrUpdate(java.lang.Object, com.witframework.core.ServiceCallback)
	 */
	public String saveOrUpdate(final Object entity,final ServiceCallback callback) throws DataAccessException {
		
		return (String) new TransactionTemplate().doInTransaction(
				new ServiceCallback(){
					public Object execute(){							
						return getDao().saveOrUpdate(cache, entity,callback);
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#saveOrUpdateAll(java.util.Collection)
	 */
	public void saveOrUpdateAll(final Collection entityCollection) throws DataAccessException {
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().saveOrUpdateAll(cache, entityCollection);
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#saveOrUpdateAll(java.util.Collection, com.witframework.core.ServiceCallback)
	 */
	public void saveOrUpdateAll(final Collection entityCollection,final ServiceCallback callback) throws DataAccessException {
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().saveOrUpdateAll(cache, entityCollection,callback);
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#update(java.lang.Object)
	 */
	public void update(final Object entity) throws DataAccessException {
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().update(cache, entity);
					}
		});
	}
	/* (non-Javadoc)
	 * @see com.witframework.core.persistence.ISimpleHibernateDao#update(java.lang.Object, com.witframework.core.ServiceCallback)
	 */
	public void update(final Object entity,final ServiceCallback callback) throws DataAccessException {
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						getDao().update(cache, entity,callback);
					}
		});
	}
	
	
}
